package com.atgugu.springcloud.controller;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.atgugu.springcloud.apis.PayFeignApi;
import com.atgugu.springcloud.entities.PayDTO;
import com.atgugu.springcloud.resp.ResultData;
import com.atgugu.springcloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping(value = "/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO){
        System.out.println("第一步：模拟本地addOrder新增订单成功（省略sql操作），第二步：调用pay服务远程调用新增支付信息");
        ResultData resultData = payFeignApi.addPay(payDTO);
        return resultData;
    }

    @GetMapping(value = "/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id){

            System.out.println("=============支付微服务远程调用，按照id查询订单流水支付信息=========");
            ResultData resultData = null;
            try {
                System.out.println("调用开始------------："+ DateUtil.now());
                 resultData = payFeignApi.getById(id);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("调用结束-------------"+DateUtil.now());
                ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
            }

            return resultData;
    }

      @GetMapping(value = "/feign/get/info")
      public String getInfoByConsul(){
        return payFeignApi.getInfoByConsul();
      }


}
